// ==========================================================================
// 云捷GO自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：2021-08-05 10:09:41
// 生成路径: app/service/module/shop/shop_service.go
// 生成人：gfcms
// ==========================================================================
package shop

import (
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gtime"
	"strconv"
	shopModel "myblog/app/model/module/shop"
	userService "myblog/app/service/system/user"
	"myblog/app/utils/convert"
	"myblog/app/utils/excel"
	"myblog/app/utils/page"
)

//根据主键查询数据
func SelectRecordById(id int64) (*shopModel.Entity, error) {
	return shopModel.FindOne("id", id)
}

//根据主键删除数据
func DeleteRecordById(id int64) bool {
	result, err := shopModel.Delete("id", id)
	if err == nil {
		affected, _ := result.RowsAffected()
		if affected > 0 {
			return true
		}
	}

	return false
}

//批量删除数据记录
func DeleteRecordByIds(ids string) int64 {
	idarr := convert.ToInt64Array(ids, ",")
	result, err := shopModel.Delete("id in (?)", idarr)
	if err != nil {
		return 0
	}

	nums, _ := result.RowsAffected()

	return nums
}

//添加数据
func AddSave(req *shopModel.AddReq, session *ghttp.Session) (int64, error) {
	var entity shopModel.Entity

	entity.Shopname = req.Shopname
	entity.Desc = req.Desc
	entity.Listorder = req.Listorder
	entity.Status = req.Status
	entity.CreateTime = gtime.Now()
	entity.CreateBy = ""

	user := userService.GetProfile(session)

	if user != nil {
		entity.CreateBy = user.LoginName
	}

	result, err := entity.Insert()
	if err != nil {
		return 0, err
	}

	id, err := result.LastInsertId()

	if err != nil || id <= 0 {
		return 0, err
	}
	return id, nil
}

//修改数据
func EditSave(req *shopModel.EditReq, session *ghttp.Session) (int64, error) {

	entity, err := shopModel.FindOne("id=?", req.Id)

	if err != nil {
		return 0, err
	}

	if entity == nil {
		return 0, gerror.New("数据不存在")
	}

	entity.Shopname = req.Shopname
	entity.Desc = req.Desc
	entity.Listorder = req.Listorder
	entity.Status = req.Status
	entity.UpdateTime = gtime.Now()
	entity.UpdateBy = ""

	user := userService.GetProfile(session)

	if user == nil {
		entity.UpdateBy = user.LoginName
	}

	result, err := entity.Save()

	if err != nil {
		return 0, err
	}

	rs, err := result.RowsAffected()

	if err != nil {
		return 0, err
	}

	return rs, nil
}

//根据条件查询数据
func SelectListAll(params *shopModel.SelectPageReq) ([]shopModel.Entity, error) {
	return shopModel.SelectListAll(params)
}

//根据条件分页查询数据
func SelectListByPage(params *shopModel.SelectPageReq) ([]shopModel.Entity, *page.Paging, error) {
	return shopModel.SelectListByPage(params)
}

// 导出excel
func Export(param *shopModel.SelectPageReq) (string, error) {
	result, err := shopModel.SelectListExport(param)
	if err != nil {
		return "", err
	}

	head := []string{"ID", "店铺名称", "店铺描述", "排序", "状态", "创建时间", "修改时间", "更新人", "创建人"}
	key := []string{"id", "shopname", "desc", "listorder", "status", "create_time", "update_time", "update_by", "create_by"}
	url, err := excel.DownlaodExcel(head, key, result)

	if err != nil {
		return "", err
	}

	return url, nil
}

func GetShopnameId() ([]shopModel.ShopId, error) {
	return shopModel.GetShopnameId()
}
func IsValueInList(id int64, ids []string) bool {
	for _, v := range ids {
		if v == strconv.FormatInt(id,10) { //64位整数转为字符串
			return true
		}
	}
	return false
}
